package com.huayi.gg.utils;

import android.os.Handler;
import android.os.Message;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class HttpClient {
    private LinkedList params = null;

    public  static String doGet(String url){
        OkHttpClient client = new OkHttpClient();
        String  str ="";
        Request request = new Request.Builder()
                .url(url)
                .build();

        try (Response response = client.newCall(request).execute()) {
            str= response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  str;
    }

    public static  String doPost(String urlpath, FormBody.Builder formBodyBuilder) {

        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(urlpath)
                .post(formBodyBuilder.build())
                .build();
        Call call = client.newCall(request);
        String result="";
        try{
            Response response = call.execute();
            result = response.body().string();

        } catch (IOException e) {
            e.printStackTrace();
        }


        return  result;

    };
    public static void uploadFile(final String actionUrl, final File uploadFile, final Handler handler, final int what) {
        new Thread(){

            @Override
            public void run() {
                super.run();
                OkHttpClient okHttpClient = new OkHttpClient();
                // file是要上传的文件 File()
                RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), uploadFile);
                // 不仅可以支持传文件，还可以在传文件的同时，传参数
                MultipartBody requestBody = new MultipartBody.Builder()
                        .setType(MultipartBody.FORM)
                        .addFormDataPart("imgFile", "data.xls", fileBody)
                        .build();
                Request request = new Request.Builder().url(actionUrl).post(requestBody).build();
                Response response = null;
                try {
                    response = okHttpClient.newCall(request).execute();
                    if(!response.isSuccessful()) {

                    }
                    String result = response.body().string();
                    response.body().close();
                    Message msg = new Message();
                    msg.what=what;
                    msg.obj = result;
                    handler.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }.start();



    }
    public static void downLoad(final String URL, final File filesDirPath, final String FILE_NAME, final Handler handler, final int what) {
        new Thread(){

            @Override
            public void run() {
                super.run();

                final OkHttpClient okHttpClient = new OkHttpClient();
                Request request = new Request.Builder()
                        .get()
                        .url(URL)
                        .build();
                Call call = okHttpClient.newCall(request);

                call.enqueue(new Callback() {
                    @Override
                    public void onFailure( Call call,  IOException e) {
                        //
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        // Log.d(TAG, "onResponse.");

                        InputStream inputStream = Objects.requireNonNull(response.body()).byteStream();
                        File target = new File(filesDirPath, FILE_NAME);
                        FileOutputStream fileOutputStream = new FileOutputStream(target);

                        try {
                            byte[] buffer = new byte[2048];
                            int len;
                            while ((len = inputStream.read(buffer)) != -1) {
                                fileOutputStream.write(buffer, 0, len);
                                // Log.d(TAG, "read: " + len);
                            }
                            fileOutputStream.flush();

                            Message msg = new Message();
                            msg.what=what;
                            msg.obj = "";
                            handler.sendMessage(msg);


                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });



            }
        }.start();



    }


}
